package com.xyjf.prod.tools.prod_tools.util;



import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;


/**
 * 
 * @author lha
 * 将requet参数组装成新的map返回给前端
 */
public class MyRequestMapUtil {
	
	
	/**
	 * 通过传递request 组装新的map集合
	 * @param request
	 * @return 返回新的map查询集合
	 */
	@SuppressWarnings("unchecked")
	public static Map<String,Object> changeMap(HttpServletRequest request){
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Map<String,Object> map=new HashMap<String,Object>();
		Map<String,String[]> reqMap=request.getParameterMap();
		Set<Entry<String,String[]>> set= reqMap.entrySet();
		for (Entry<String, String[]> entry : set) {
			 if(entry.getValue()!=null&&entry.getValue().length==1){
				if(StringUtils.isNotEmpty(entry.getValue()[0])){
					map.put(entry.getKey(), entry.getValue()[0]);
				}
			//如果长度不为1则以逗号的形式隔开
			}else if(entry.getValue()!=null&&entry.getValue().length>1){
			/*	if(StringUtils.isNotEmpty(entry.getValue()[0])){
					map.put(entry.getKey(), entry.getValue()[0]);
				}*/
				String value="";
				for (String str : entry.getValue()) {
					value+=str+",";
				}
				value=value.substring(0,value.length()-1);
				map.put(entry.getKey(),value);
			}
		}
		return map;
	}
	
	
	
	/**
	 * 通过传递request 组装新的新增map集合
	 * @param request request 请求
	 * @return 返回新的保存map集合
	 */
	public static Map<String,Object> changeSaveMap(HttpServletRequest request){
		Map<String,Object> map=changeMap(request);
		return map;
	}
	
	
	
	
	
	public static Map<String,Object> changeSaveMap(Map<String,Object> map,HttpServletRequest request){
		return map;
	}
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 通过传递request 组装新的编辑map集合
	 * @param request request 请求
	 * @return 返回新的编辑map集合
	 */
	public static Map<String,Object> changeEditMap(HttpServletRequest request){
		Map<String,Object> map=changeMap(request);
		map.put("updateTime", new Date());
		return map;
	}
	
	
	
	public static void formatTimeInRequestMap(Map<String, Object> map,String format,String...strings ) {
	}

	
}
